package com.penngo.gui.dfdiff.view;

import com.penngo.gui.dfdiff.DFMain;
import com.penngo.gui.dfdiff.utils.Config;
import com.penngo.gui.dfdiff.utils.FontUtil;
import com.penngo.gui.dfdiff.utils.GB;
import com.penngo.gui.dfdiff.utils.LogUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;

public class FontDialog extends BaseDialog {
    private Logger log = LogUtils.getLogger(FontDialog.class);

    private LinkedHashMap fontMap;
    private JList<String> fontList;
    private JList<Integer> styleList;
    private JList<Integer> sizeList;
    private JLabel exampleLabel;
    private JButton saveButton;
    public FontDialog() {
        //this.setLayout(new GridBagLayout());
        setSize(600,300);
    }

    public void initUI(){
        setFont(Config.getFont());
        setSize(600,500);
        setTitle("选择字体");
        ItemMouseAdapter itemMouseAdapter = new ItemMouseAdapter();

        fontList = initFontList();
        fontList.addMouseListener(itemMouseAdapter);
        add(new JScrollPane(fontList), new GB(0, 0, 1,1).setWeight(40, 100).setFill(GB.BOTH).setInsets(10, 10, 10, 10));

        styleList = initStyleList();
        styleList.addMouseListener(itemMouseAdapter);
        add(new JScrollPane(styleList), new GB(1, 0, 1,1).setWeight(30, 100).setFill(GB.BOTH).setInsets(10, 10, 10, 10));

        sizeList = initSizeList();
        sizeList.addMouseListener(itemMouseAdapter);
        add(new JScrollPane(sizeList), new GB(2, 0, 1,1).setWeight(30, 100).setFill(GB.BOTH).setInsets(10, 10, 10, 10));

        exampleLabel = initExampleText();
        add(exampleLabel, new GB(0,1,3,1).setIpad(10, 20));

        JPanel buttonPanel = initButton();
        add(buttonPanel, new GB(0,2,3,1).setFill(GB.BOTH).setWeight(100,0));
    }

    private JList initFontList(){
//        String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        String[] fonts = FontUtil.getFonts();// GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.forLanguageTag("zh_CN"));
        fontMap = new LinkedHashMap<>();
        for (String value : fonts) {
            fontMap.put(value, value);
        }

        DefaultListModel namesListModel = new DefaultListModel<>();
        namesListModel.addAll(fontMap.keySet().stream().toList());

        JList jlist = new JList(namesListModel);
        jlist.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

            }
        });
        return jlist;
    }

    class ItemMouseAdapter extends MouseAdapter{
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            String font = fontList.getSelectedIndex() > -1 ? fontList.getSelectedValue() : Config.getFont().getFontName();
            int style = styleList.getSelectedIndex() > -1 ? styleList.getSelectedIndex() : 0;
            int size = sizeList.getSelectedIndex() > -1 ? sizeList.getSelectedValue() : 12;
            Font f = new Font(font, style, size);
            exampleLabel.setFont(f);
        }
    }


    private JList initStyleList(){
        String[] styles = {"PLAIN", "BOLD", "ITALIC"};
        JList<String> jlist = new JList<>(styles);
        return jlist;
    }

    private JList initSizeList(){
        List<Integer> s = new ArrayList<>();
        for (int i = 8; i <= 96; i += 2)
            s.add(i);
        Integer[] sizes = s.toArray(new Integer[0]);
        JList<Integer> jlist = new JList<>(sizes);
        return jlist;
    }
    private JLabel initExampleText(){
        JLabel example = new JLabel("示例文字 Example!");

        // 初始化示例
        Font defaultFont = Config.getFont();
        String fontName = defaultFont.getName();
        int style = defaultFont.getStyle();
        int size = defaultFont.getSize();
        fontList.setSelectedValue(fontName, true);
        styleList.setSelectedIndex(style);
        sizeList.setSelectedValue(size, true);
        example.setFont(defaultFont);
        return example;
    }
    private JPanel initButton(){
        JPanel jpane = new JPanel(new GridBagLayout());

        jpane.add(new JPanel(), new GB(0,0,1,1).setFill(GB.BOTH).setWeight(100,0));

        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(e -> {
            String fontName = fontList.getSelectedValue();
            int style = styleList.getSelectedIndex();
            int size = sizeList.getSelectedValue();
            Font defaultFont = new Font(fontName, style, size);
            Config.setFont(defaultFont);
            updateEditFrame();
            dispose();
        });

        jpane.add(saveButton, new GB(1,0,1,1).setInsets(10));

        return jpane;
    }

    private void updateEditFrame(){
        Component[] components = DFMain.panelRight.getAllComponent();
        for(Component comp:components){
            if (comp instanceof RightEditor editor){
                editor.updateFont(Config.getFont());
            }
        }
//        JInternalFrame[] farmes = DFMain.desktopPane.getAllFrames();
//        for(JInternalFrame famre:farmes){
//            if(famre instanceof DesktopFrameEditor editor){
//                editor.updateFont(Config.getFont());
//            }
//        }
    }
}
